package com.san.simulation.impl;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.impl.bpmn.parser.BpmnParse;
import org.activiti.engine.impl.bpmn.parser.BpmnParseListener;
import org.activiti.engine.impl.bpmn.parser.BpmnParser;
import org.activiti.engine.impl.el.ExpressionManager;
import org.activiti.engine.impl.persistence.entity.DeploymentEntity;

import com.san.simulation.api.ProcessDeployer;
import com.san.simulation.listener.SimulationParseListener;
import com.san.simulation.model.api.ProcessDefinition;
import com.san.simulation.model.impl.ActivitiProcessDefinition;

public class ActivitiProcessDeployer implements ProcessDeployer{
	

	public BpmnParse buildProcessDefinition(String resource) {

		BpmnParser parser = new BpmnParser(new ExpressionManager());
		
		List<BpmnParseListener> listeners = parser.getParseListeners();
		if (listeners == null) {
			listeners = new ArrayList<BpmnParseListener>();
		}
		listeners.add(new SimulationParseListener());
		
		parser.setParseListeners(listeners);
		BpmnParse bpmnParse = parser.createParse();
		if (resource != null) {
			bpmnParse.sourceResource(resource);
		}
		
		bpmnParse.deployment(new DeploymentEntity());
		bpmnParse.execute();
		return bpmnParse;
	}

	public ProcessDefinition getProcessDefinitions(BpmnParse parse) {
		
		ProcessDefinition activitiDefinition= null;
		
		if(parse.getProcessDefinitions()!=null && parse.getProcessDefinitions().get(0)!=null){
			activitiDefinition = new ActivitiProcessDefinition(parse.getProcessDefinitions().get(0));
		}
		
		return activitiDefinition;
	}

}
